import {
	mapMutations
} from 'vuex'

export default {
	...mapMutations([
		'upToken',
		'upUserInfo',
		'upRefreshFlag'
	]),
	handlerChoose() {
		this.isChecked = !this.isChecked
	},
	//用户协议
	toUserAgreement() {
		uni.navigateTo({
			url: '/pages/publics/userAgreement'
		})
	},
	privacyAgreement() {
		uni.navigateTo({
			url: '/pages/publics/privacyAgreement'
		})
	},
	toOtherLogin() {
		uni.navigateTo({
			url: '/pages/publics/phoneLogin'
		})
	},
	//用户协议

	toRechargeAgreement() {
		let that = this
		if (that.atLoading) {
			//禁止多次点击
			return false
		}
		that.atLoading = true
		uni.navigateTo({
			url: '/pages/my/userAgreement'
		})
		setTimeout(() => { //延时指定时间后，再解冻
			that.atLoading = false;
		}, 1000);
	},
	toLogin() {
		let that = this
		if (!this.isChecked) {
			this.$toast.msg('请阅读并同意协议', {
				icon: 1
			})
			return false
		}
		uni.getUserProfile({
			desc: '登录',
			success: (res1) => {
				that.hasInfo = !that.hasInfo
				that.info = res1
				console.log(res1, "获取个人信息")
			},
			fail: (err) => {
				console.log(err);
			}
		})
		wx.vibrateShort()
	},
	getUserInfo() {
		let that = this
		that.$httpRequest.get({
			that: that,
			url: 'wechat/info',
			params: {

			},
			success: res => {
				// console.log(res,"在login获取用户信息")
				if (res.code === that.$httpConfig.SUCCESS_CODE) {
					that.upUserInfo(res.data)
				} else {
					that.upUserInfo({})
				}
			}
		})
	},
	getLoginphone(e) {
		console.log("手机信息", e)
		let that = this
		uni.login({
			success: (res_login) => {
				console.log("登录信息", res_login)
				that.$httpRequest.request({
					url: 'wechat/mobileLogin',
					params: {
						loginCode: res_login.code,
						phoneCode: e.detail.code,
						userName: that.info.userInfo.nickName,
						avatar: that.info.userInfo.avatarUrl,
						gender: parseInt(that.info.userInfo.gender + 1),
						shareUserId: uni.getStorageSync('shareUserId') || null
					},
					success: res => {
						// that.loading = false
						if (res.code === that.$httpConfig.SUCCESS_CODE) {

							that.hasInfo = !that.hasInfo
							// that.upTipsFlag('notShow')

							uni.setStorageSync('token', res.data.token)
							that.upToken(res.data.token)
							// that.$emit('refresh') //刷新
							that.upRefreshFlag(true)//刷新
							uni.switchTab({
								url: '/pages/my/index'
							})
							that.$nextTick(function() {
								that.getUserInfo();
							})


						} else {
							that.$toast.alert({
								content: res.message,
								callback: result => {

								}
							})
						}
					}
				})
			}
		})
		wx.vibrateShort()
	},
	submitForm() {
		let that = this
		if (that.loading) {
			that.$toast.msg('请求中...', {
				icon: 1
			})
			return false
		}
		that.loading = true
		that.$httpRequest.request({
			that: that,
			url: 'Publics/login',
			params: {
				mobile: that.formData.mobile,
				code: that.formData.code
			},
			success: res => {
				that.loading = false
				if (res.code === that.$httpConfig.SUCCESS_CODE) {
					that.upToken(res.data.token)
					let url = ''
					url = '/pages/index/index'
					uni.switchTab({
						url
					})
				} else {
					that.$toast.alert({
						content: res.msg,
						callback: result => {

						}
					})
				}
			}
		})

	},
	toReturn() {
		uni.navigateBack({
			delta: 1 // 返回上一级页面。
		})
	}
}